package org.csu.sei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.sei.entity.Category;
import org.csu.sei.entity.Title;
import org.csu.sei.persistence.CategoryMapper;
import org.csu.sei.persistence.TitleMapper;
import org.csu.sei.service.ITitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component("titleService")
public class TitleServiceimpl extends ServiceImpl<TitleMapper, Title> implements ITitleService {

    @Autowired
    TitleMapper titleMapper;

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public List<Title> queryAllTitle() {
        List<Title> titleList=titleMapper.selectList(null);
        return titleList;
    }

    @Override
    public int changeName(Long id,String name)
    {
        Title title=titleMapper.selectById(id);
        if (!title.getTitleName().equals(name))
        {
            title.setTitleName(name);
            return titleMapper.updateById(title);
        }
        return 0;
    }

    @Override
    public void checkName(Long id)
    {
        System.out.println(categoryMapper.selectCount(new QueryWrapper<Category>().eq("title_id",id)));
        if(categoryMapper.selectCount(new QueryWrapper<Category>().eq("title_id",id))==1)
        {
            String name = categoryMapper.selectList(new QueryWrapper<Category>().eq("title_id",id)).get(0).getName();
            Title title = titleMapper.selectById(id);
            title.setTitleName(name);
            titleMapper.updateById(title);
        }
    }
}
